#include<iostream>
#include<unistd.h>
#include<cstdlib>
#include<string>
#include"mysql/mysql.h"

#include"Common.hpp"

MYSQL* ConnectMySql()
{
    MYSQL* conn=mysql_init(nullptr);
    mysql_set_character_set(conn,"utf8");
    if(mysql_real_connect(conn,"127.0.0.1","http_client","asd144877","http_db",3306,nullptr,0)==nullptr)
    {
        std::cerr<<"connect error"<<std::endl;
        return nullptr;
    }
    std::cerr<<"connect success"<<std::endl;
    return conn;
}

bool InsertSql(std::string sql)
{
    MYSQL*conn=ConnectMySql();
    
    std::cerr<<"query_string: "<<sql<<std::endl;
    int ret=mysql_query(conn,sql.c_str());
    std::cerr<<"result: "<<ret<<std::endl;
    if(ret==0)
    {
        std::cout<<"<meta charset=\"utf-8\">"<<std::endl;
        std::cout<<"<h1 style=\"color:firebrick;text-align:center\">注册成功</h1>"<<std::endl;
    }
    mysql_close(conn);
    return true;
}

bool DeleteSql(std::string sql)
{
    MYSQL*conn=ConnectMySql();
    
    std::cerr<<"query_string: "<<sql<<std::endl;
    int ret=mysql_query(conn,sql.c_str());
    std::cerr<<"result: "<<ret<<std::endl;
    if(ret==0)
    {
        std::cout<<"<meta charset=\"utf-8\">"<<std::endl;
        std::cout<<"<h1 style=\"color:firebrick;text-align:center\">删除成功</h1>"<<std::endl;
    }
    mysql_close(conn);
    return true;
}

bool UpdateSql(std::string sql)
{
    MYSQL*conn=ConnectMySql();
    
    std::cerr<<"query_string: "<<sql<<std::endl;
    int ret=mysql_query(conn,sql.c_str());
    std::cerr<<"result: "<<ret<<std::endl;
    if(ret==0)
    {
        std::cout<<"<meta charset=\"utf-8\">"<<std::endl;
        std::cout<<"<h1 style=\"color:firebrick;text-align:center\">修改成功</h1>"<<std::endl;
    }
    mysql_close(conn);
    return true;
}
bool SelectSql(std::string sql)
{
    MYSQL*conn=ConnectMySql();
    int ret=mysql_query(conn,sql.c_str());
    if(ret==0)
    {
        MYSQL_RES * res=mysql_store_result(conn);
        my_ulonglong  rows=mysql_num_rows(res);
        int cols = mysql_num_fields(res);
        MYSQL_FIELD *field = mysql_fetch_fields(res);
        int i = 0;
        std::cout<<"<html><body><meta charset=\"utf-8\">";
        for(; i < cols; i++)
        {
            std::cout<<field[i].name<<" ";
        }
        std::cout<<"<br>"<<std::endl;
        i = 0;
        MYSQL_ROW line;
        for(; i < rows; i++)
        {
            line = mysql_fetch_row(res);
            int j = 0;
            for(; j < cols; j++)
            {
                std::cout<<line[j]<<" ";
            }
            std::cout<<"<br>"<<std::endl;
        }
        //std::cout<<"</html>";
        free(res);
    }else
    {
        std::cout<<"<h1 style=\"color:firebrick;text-align:center\">查询失败</h1>"<<std::endl;
    }
    mysql_close(conn);
    return true;
}




int main()
{
    std::string query_string;
    if(GetQueryString(query_string))
    {
        std::cerr<<"query_string: "<<query_string<<std::endl;
        std::string query_opt,opt,query,query1,query2,tmp_opt;
        CutString(query_string,"&",query_opt,query);
        CutString(query_opt,"=",tmp_opt,opt);
        CutString(query,"&",query1,query2);
        std::cerr<<opt<<" : "<<query1<<" : "<<query2;

        if(opt=="insert")
        {
            std::string name,password,tmp;
            CutString(query1,"=",tmp,name);
            CutString(query2,"=",tmp,password);
            std::string sql="insert into user(name,password) values (\'";
            sql+=name;
            sql+="\',\'";
            sql+=password;
            sql+="\');";
            InsertSql(sql);
        }else if(opt=="delete")
        {
            std::string name,password,tmp;
            CutString(query1,"=",tmp,name);
            CutString(query2,"=",tmp,password);
            std::string sql="delete from user where name=\'";
            sql+=name;
            sql+="\';";

            DeleteSql(sql);
        }else if(opt=="update")
        {
            std::string name,password,tmp;
            CutString(query1,"=",tmp,name);
            CutString(query2,"=",tmp,password);
            std::string sql="update user set name=\'";
            sql+=name;
            sql+="\',password=\'";
            sql+=password;
            sql+="\' ";
            sql+="where name =\'";
            sql+=name;
            sql+="\';";
            UpdateSql(sql);
        }else if(opt=="select")
        {
            std::string name,password,tmp;
            CutString(query1,"=",tmp,name);
            CutString(query2,"=",tmp,password);
            std::string sql;
            if(name=="")
            {
                sql="select * from user";
            }else
            {
                sql="select * from user where name=\'";
                sql+=name;
                sql+="\'";
            }
            
            
            SelectSql(sql);
        }
        
        
    }
    //query_string="select * from user where id=3;";
    //SelectSql(query_string);

   return 0;
}
